package com.wxmp.core.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;

public class posterUtils {

    public static void main(String[] args) {
        //for (int i = 0; i < 1000; i++) {
        String backgroundPath = "D:\\gimborn\\background.jpg";
        String qrCodePath = "D:\\gimborn\\qrcode.jpg";
        String message01 = "董咚咚";
        String message02 = "昵称[董咚咚]";
        String outPutPath = String.format("%s%s%s", "D:\\gimborn\\", 1, ".jpg");
        overlapImage(backgroundPath, qrCodePath, message01, message02, outPutPath);
        //}
    }

    /**
     * 海报图片合成
     *
     * @param backgroundPath 背景图片固定
     * @param qrCodePath     二维码图片传递进来
     * @param message01
     * @param message02
     * @param outPutPath
     * @return
     */
    public static String overlapImage(String backgroundPath, String qrCodePath, String message01, String message02, String outPutPath) {
        try {
            //设置图片大小
//          BufferedImage background = resizeImage(618,1000, ImageIO.read(new File("这里是背景图片的路径！")));
            BufferedImage background = resizeImage(500, 900, ImageIO.read(new File(backgroundPath)));
//          BufferedImage qrCode = resizeImage(150,150,ImageIO.read(new File("这里是插入二维码图片的路径！")));
            BufferedImage qrCode = resizeImage(150, 150, ImageIO.read(new File(qrCodePath)));
            //String message = "董咚咚";
            Graphics2D g = background.createGraphics();

            //new Color(51, 51, 51)
            g.setColor(new Color(51, 51, 51));
            g.setFont(new Font("微软雅黑", Font.TRUETYPE_FONT, 18));
            //文字描述
            //region 文字描述及定位
            int textLength = message01.length();
            //文字X轴定位
            int xIndex = 110;
            String newText = message01;
            if (textLength > 5) {
                xIndex = 90;
                newText = String.format("%s%s", message01.substring(0, 5), "*");
            } else if (textLength == 5)
                xIndex = 100;
            else if (textLength == 4) {
                xIndex = 110;
            } else if (textLength == 3)
                xIndex = 120;
            else if (textLength == 2)
                xIndex = 140;

            g.drawString(newText, xIndex, 755);
            //endregion
            //文字昵称
            //g.drawString(message02, 530, 220);
            //在背景图片上添加二维码图片
            g.drawImage(qrCode, 300, 676, qrCode.getWidth(), qrCode.getHeight(), null);
            g.dispose();
//            ImageIO.write(background, "jpg", new File("这里是一个输出图片的路径"));
            ImageIO.write(background, "jpg", new File(outPutPath));
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static BufferedImage resizeImage(int x, int y, BufferedImage bfi) {
        BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
        bufferedImage.getGraphics().drawImage(
                bfi.getScaledInstance(x, y, Image.SCALE_SMOOTH), 0, 0, null);
        return bufferedImage;
    }
}
